home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / Programming Contest / Secret Folder / Problem 02 / Secret.cp < prev    next >
Encoding:
Text File  |  1998-06-11  |  2.8 KB  |  124 lines  |  [TEXT/CWIE]

  1. #include "Solution.h"
  2. #include "ProblemUtils.h"
  3.  
  4. #include <stdio.h>
  5. #include <Files.h>
  6. #include <Errors.h>
  7.  
  8. #define MAX_LINE_LEN 200
  9. #define MAX_DISKS 1000
  10.  
  11. static OSErr ReadUInt32FromHandle( Handle data, UInt32 *number )
  12. {
  13.     OSErr err;
  14.     char line[MAX_LINE_LEN];
  15.     char *p;
  16.  
  17.     p = line;    
  18.     if ( ProblemReadLineFromHandle( data, line, MAX_LINE_LEN ) && ProblemGetUInt32( &p, number ) && *p == 0 ) {
  19.         err = noErr;
  20.     } else {
  21.         err = -1;
  22.     }
  23.     return err;
  24. }
  25.  
  26. static OSErr Read2UInt32FromHandle( Handle data, UInt32 *number1, UInt32 *number2 )
  27. {
  28.     OSErr err;
  29.     char line[MAX_LINE_LEN];
  30.     char *p;
  31.  
  32.     p = line;    
  33.     if ( ProblemReadLineFromHandle( data, line, MAX_LINE_LEN ) ) {
  34.         if ( ProblemGetUInt32( &p, number1 )
  35.             && ProblemGetUInt32( &p, number2 ) && *p == 0 ) {
  36.             err = noErr;
  37.         } else {
  38.             err = -1;
  39.         }
  40.     } else {
  41.         err = noErr;
  42.         *number1 = 0;
  43.         *number2 = 0;
  44.     }
  45.     return err;
  46. }
  47.  
  48. typedef struct Pole {
  49.     UInt32 count;
  50.     UInt32 disks[MAX_DISKS];
  51. } Pole;
  52.  
  53. pascal OSErr CheckHowerOfTanoi( const FSSpec* infile, const FSSpec* outfile, Boolean *correct )
  54. {
  55.     OSErr err;
  56.     Handle indata, outdata;
  57.     UInt32 disk_count;
  58.     UInt32 from;
  59.     UInt32 to;
  60.     Pole poles[3];
  61.     UInt32 i;
  62.     
  63.     *correct = false;
  64.     
  65.     err = HowerOfTanoi( infile, outfile );
  66.     if ( err == noErr ) {
  67.         err = ProblemFileRead( infile, &indata );
  68.         if ( err == noErr ) {
  69.             err = ProblemFileRead( outfile, &outdata );
  70.             if ( err == noErr ) {
  71.  
  72.                 err = ReadUInt32FromHandle( indata, &disk_count );
  73.                 if ( err == noErr ) {
  74.                     poles[0].count = disk_count;
  75.                     poles[1].count = 0;
  76.                     poles[2].count = 0;
  77.                     for ( i = 1; i <= disk_count; i++ ) {
  78.                         err = ReadUInt32FromHandle( indata, &poles[0].disks[disk_count-i] );
  79.                         if ( err != noErr ) break;
  80.                     }
  81.                 }
  82.             
  83.                 if ( err == noErr ) {
  84.                     *correct = true;
  85.                     while ( *correct ) {
  86.                         err = Read2UInt32FromHandle( outdata, &from, &to );
  87.                         if ( err != noErr ) break;
  88.                         if ( from == 0 && to == 0 ) {
  89.                             break;
  90.                         }
  91.                         if ( from < 1 || from > 3 || to < 1 || to > 3 || from == to || poles[from-1].count == 0 ) {
  92.                             *correct = false;
  93.                             break;
  94.                         }
  95.                         if ( poles[to-1].count > 0 && poles[to-1].disks[poles[to-1].count - 1] < poles[from-1].disks[poles[from-1].count - 1] ) {
  96.                             *correct = false;
  97.                             break;
  98.                         }
  99.                         poles[to-1].count++;
  100.                         poles[to-1].disks[poles[to-1].count - 1] = poles[from-1].disks[poles[from-1].count - 1];
  101.                         poles[from-1].count--;
  102.                     }
  103.                 }
  104.                 
  105.                 if ( err == noErr && *correct ) {
  106.                     *correct = (poles[0].count == 0 && poles[1].count == 0 && poles[2].count == disk_count);
  107.                     if ( *correct ) {
  108.                         for ( i = 1; i <= disk_count; i++ ) {
  109.                             if ( poles[2].disks[i-1] != disk_count - i + 1 ) {
  110.                                 *correct = false;
  111.                                 break;
  112.                             }
  113.                         }
  114.                     }
  115.                 }
  116.             
  117.                 DisposeHandle( outdata );
  118.             }
  119.             DisposeHandle( indata );
  120.         }
  121.     }
  122.     return err;
  123. }
  124.